package com.glsc.ngateway.platform.controller.itflow;

import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.flowable.dto.FlowDoTaskRespDto;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.equipment.*;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.equipment.EquipmentServerApplyTaskFormDataDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.equipment.domainDto.FlowFormEquipmentServerApplyDto;
import com.glsc.ngateway.common.api.flowable.resp.FlowableResponse;
import com.glsc.ngateway.common.api.flowable.resp.ResponseMsgDto;
import com.glsc.ngateway.common.api.flowable.resp.ResponseSituationDto;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.platform.service.itflow.ItFlowService;
import com.glsc.ngateway.platform.utils.RequestTool;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author xuchang
 */
@RestController
@RequestMapping(value = "/it/equipment/serverApply")
@Api(value = "设备管理-物理设备申领及上架流程接口", tags = {"设备管理-物理设备申领及上架流程接口"})
public class FlowEquipmentServerApplyController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ItFlowService itFlowService;

    @Resource
    private RequestTool requestTool;

    @ApiOperation(value = "流程发起", notes = "流程发起")
    @PostMapping("/createFlow")
    public PlatformResponse<FlowDoTaskRespDto> createFlow(@RequestBody EquipmentCreateParamDto<FlowFormEquipmentServerApplyDto, EquipmentServerApplyTaskFormDataDto> param) throws Exception {
        String operatorStart = requestTool.getAccountFromRequest();
        FlowDoTaskRespDto respDto = itFlowService.createFlow(param, operatorStart, ITFlowEnum.EQUIPMENT_SERVER_APPLY.getKey());
        return PlatformResponse.successData(respDto);
    }

    @ApiOperation(value = "保存节点数据", notes = "保存节点数据")
    @PostMapping("/saveFormData")
    public PlatformResponse<FlowDoTaskRespDto> saveFormData(@RequestBody EquipmentCreateParamDto<FlowFormEquipmentServerApplyDto, EquipmentServerApplyTaskFormDataDto> param) throws Exception {
        String operatorStart = requestTool.getAccountFromRequest();
        FlowDoTaskRespDto respDto = itFlowService.saveFormData(param, operatorStart, ITFlowEnum.EQUIPMENT_SERVER_APPLY.getKey());
        return PlatformResponse.successData(respDto);
    }

    @ApiOperation(value = "审核通过", notes = "流程审核处理通过")
    @PostMapping("/taskAuditPass")
    public PlatformResponse<FlowDoTaskRespDto> taskAuditPass(@RequestBody EquipmentPassParamDto<FlowFormEquipmentServerApplyDto, EquipmentServerApplyTaskFormDataDto> param) throws Exception {
        String operatorStart = requestTool.getAccountFromRequest();
        FlowDoTaskRespDto respDto = itFlowService.taskAuditPass(param, operatorStart, ITFlowEnum.EQUIPMENT_SERVER_APPLY.getKey());
        return PlatformResponse.successData(respDto);
    }

    @Deprecated
    @ApiOperation(value = "审核驳回，返回至起始节点", notes = "流程审核驳回处理")
    @PostMapping("/taskAuditReject")
    public PlatformResponse<FlowableResponse<ResponseMsgDto>> taskAuditReject(@RequestBody EquipmentRejectParamDto taskDto) throws PlatformException {
        FlowableResponse<ResponseMsgDto> respDto = itFlowService.taskAuditReject(taskDto, requestTool.getAccountFromRequest(), ITFlowEnum.EQUIPMENT_SERVER_APPLY.getKey());
        return PlatformResponse.successData(respDto);
    }

    @ApiOperation(value = "根据流程实例ID或者任务ID检索流程", notes = "根据流程实例ID或者任务ID检索流程状态、备注等，如提供流程实例ID则认为只读方式获取数据")
    @PostMapping("/findByProcessIdOrTaskId")
    public PlatformResponse<EquipmentCreateParamDto<FlowFormEquipmentServerApplyDto, EquipmentServerApplyTaskFormDataDto>> findByProcessIdOrTaskId(@RequestBody EquipmentFindByIdParamDto param) {
        EquipmentCreateParamDto<FlowFormEquipmentServerApplyDto, EquipmentServerApplyTaskFormDataDto> context = new EquipmentCreateParamDto<>();
        itFlowService.getWorkFlowFormByTaskId(param, ITFlowEnum.EQUIPMENT_SERVER_APPLY.getKey(), context);
        return PlatformResponse.successData(context);
    }

    @ApiOperation(value = "流程分页查询", notes = "流程分页查询")
    @GetMapping("/findProcessPage")
    public PlatformResponse<Page<ResponseSituationDto>> getProcessSituationPage(
            @ApiParam(value = "流程查询参数") @RequestBody EquipmentFlowSearchParamDto param) {
        String operatorStart = requestTool.getAccountFromRequest();
        Page<ResponseSituationDto> page = itFlowService.getProcessSituationPage(param, operatorStart, ITFlowEnum.EQUIPMENT_SERVER_APPLY.getKey());
        return PlatformResponse.successData(page);
    }
}
